iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

30天菜鳥學 Linux 系列 第 31

31天-繼續學習 docker build

  • 分享至 

  • xImage
  •  

今天閱讀 : Build and run your image | Docker Documentation

test@test:~$ git clone https://github.com/dockersamples/node-bulletin-board
Cloning into 'node-bulletin-board'...
remote: Enumerating objects: 124, done.
remote: Total 124 (delta 0), reused 0 (delta 0), pack-reused 124
Receiving objects: 100% (124/124), 185.00 KiB | 31.00 KiB/s, done.
Resolving deltas: 100% (54/54), done.
test@test:~$ cd node-bulletin-board/bulletin-board-app
test@test:~/node-bulletin-board/bulletin-board-app$ ls
app.js  backend  Dockerfile  fonts  index.html  LICENSE  package.json  readme.md  server.js  site.css
test@test:~/node-bulletin-board/bulletin-board-app$ cat Dockerfile 
FROM node:current-slim

WORKDIR /usr/src/app
COPY package.json .
RUN npm install

EXPOSE 8080
CMD [ "npm", "start" ]

要切換 sudo 否則會出現這樣錯誤

COPY . .test@test:~/node-bulletin-board/bulletin-board-app$ docker build --tag bulletinboard:1.0 .
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&session=vxrjn054b1lkm024tyt49hwas&shmsize=0&t=bulletinboard%3A1.0&target=&ulimits=null&version=1: dial unix /var/run/docker.sock: connect: permission denied
test@test:~/node-bulletin-board/bulletin-board-app$ 

使用 sudo -s 切換後

root@test:/home/test/node-bulletin-board/bulletin-board-app# docker build --tag bulletinboard:1.0 .
Sending build context to Docker daemon  45.57kB
Step 1/7 : FROM node:current-slim
current-slim: Pulling from library/node
abb454610128: Pull complete 
3dfc5a66c517: Pull complete 
33b46fd32a07: Pull complete 
d040c2a42822: Pull complete 
a56d75ef4840: Pull complete 
Digest: sha256:784f4398dcd0fc6afa19f2d426d98ea07f8a51e5aa495d3b191b7784c37e6bd0
Status: Downloaded newer image for node:current-slim
 ---> 855a1cd7a580
Step 2/7 : WORKDIR /usr/src/app
 ---> Running in a31a733e0cf2
Removing intermediate container a31a733e0cf2
 ---> f1f4172fd2ef
Step 3/7 : COPY package.json .
 ---> 286cd8ec277f
Step 4/7 : RUN npm install
 ---> Running in c67837d9f180

> ejs@2.7.4 postinstall /usr/src/app/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN vue-event-bulletin@1.0.0 No repository field.
npm WARN The package morgan is included as both a dev and production dependency.

added 91 packages from 168 contributors and audited 92 packages in 10.633s
found 0 vulnerabilities

Removing intermediate container c67837d9f180
 ---> 11c58a6b3b30
Step 5/7 : EXPOSE 8080
 ---> Running in 72a2fb9b632c
Removing intermediate container 72a2fb9b632c
 ---> 4d8476270b7e
Step 6/7 : CMD [ "npm", "start" ]
 ---> Running in e5031e111318
Removing intermediate container e5031e111318
 ---> 730c1456e1ed
Step 7/7 : COPY . .
 ---> 8f95bffb5f12
Successfully built 8f95bffb5f12
Successfully tagged bulletinboard:1.0

接著 run build 好的 image 用 curl 做驗證

root@test:/home/test/node-bulletin-board/bulletin-board-app# docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0
19723e2517d954433c39aff736c12955157d5ad82a2a272a2996fb623cb2a6dc
root@test:/home/test/node-bulletin-board/bulletin-board-app# curl localhost:8000
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Bulletin Board</title>
  <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="site.css">
  <style>
  .form-control {
    margin-bottom: 10px;
  }
  .btn-danger {
    margin-top: 8px;
  }
  </style>
</head>
<body>

  <div class="jumbotron">
    <h1>Welcome to the Bulletin Board</h1>
  </div>

  <div class="container" id="events">
    <div class="col-sm-7">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h3>Add an Event</h3>
        </div>
        <div class="panel-body">
          <div>
            <input class="form-control" placeholder="Title" v-model="event.title">
            <textarea class="form-control" placeholder="Detail" v-model="event.detail"></textarea>
            <input type="date" class="form-control" placeholder="Date" v-model="event.date">
            <button class="btn btn-primary" v-on:click="addEvent">Submit</button>
          </div>
        </div>
      </div>
    </div>
    <div class="col-sm-5">
      <div class="list-group">
        <a href="#" class="list-group-item" v-for="event in events">
          <h4 class="list-group-item-heading"><i class="glyphicon glyphicon-bullhorn"></i> {{ event.title }}</h4>
          <h5><i class="glyphicon glyphicon-calendar" v-if="event.date"></i> {{ event.date }}</h5>
          <p class="list-group-item-text" v-if="event.detail">{{ event.detail }}</p>
          <button class="btn btn-xs btn-danger" v-on:click="deleteEvent(event.id)">Delete</button>
        </a>
      </div>
    </div>
  </div>

</body>
<script src="node_modules/vue/dist/vue.min.js"></script>
<script src="node_modules/vue-resource/dist/vue-resource.min.js"></script>
<script src="app.js"></script>

接著刪除掉 running 的 container

root@test:/home/test/node-bulletin-board/bulletin-board-app# docker rm --force bb
bb
root@test:/home/test/node-bulletin-board/bulletin-board-app# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

上一篇
30天-學會如何查 port 的占用狀況
下一篇
32天-學習建立 Node image
系列文
30天菜鳥學 Linux 59
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言